<template>
	<view class="container">
		<nav-top :title="navTitle" :type="navType" />
		<view class="content">
			<view class="form">
				<view class="user-name item">
					<view>用户名</view>
					<input type="text" v-model="newAddressInformation.name">
				</view>
				<view class="gender item">
					<view>性别</view>
					<radio-group @change="genderChange">
						<label>
							<radio color="#FFD93B" :value="'MALE'" checked="checked" />先生
							<radio color="#FFD93B" :value="'FEMALE'" />女士
						</label>
					</radio-group>
				</view>
				<view class="phone-number item">
					<view class="importent">电话号码</view>
					<input type="number" v-model="newAddressInformation.tel">
				</view>
				<view class="detail-address item">
					<view>详细地址</view>
					<input type="text" v-model="newAddressInformation.detailAddress">
				</view>
				<view class="default-address item">
					<view class="importent">默认地址</view>
					<switch :checked="newAddressInformation.isDefault" color="#FFD93B" @change="changeDefault" />
				</view>
			</view>

			<view class="button-group">
				<button v-if="addOrChange==='change'" class="delete" @click="deleteAddress">删除</button>
				<button class="submit" @click="SubmitEvent">{{ addOrChange==="change"?"保存":"新增地址" }}</button>

			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import request from "@/Api/request.js"
	import store from "@/store/index.js"
	const navTitle = "添加地址";
	const navType = "commen";
	const addOrChange = ref("")
	let oldAddress = {};
	let updateAddressId;
	const newAddressInformation = reactive({
		name: "",
		gender: "MALE",
		tel: "",
		detailAddress: "",
		isDefault: false
	})

	function genderChange(e) {
		console.log(e.target.value);
		newAddressInformation.gender = e.target.value
	}

	function changeDefault(e) {
		console.log(e.target.value);
		newAddressInformation.isDefault = e.target.value
	}

	function deleteAddress() {
		request({
			url:"/address/" + updateAddressId,
			method:"DELETE",
		}).then((res)=>{
		})
	}

	function SubmitEvent() {
		if (addOrChange.value === "add") {
			const params = {
					ad:newAddressInformation.detailAddress,
					def:newAddressInformation.isDefault,
					gender:newAddressInformation.gender,
					name:newAddressInformation.name,
					phone:newAddressInformation.tel,
					userId:store.state.user.id
				}
			request({
				url:"/address",
				method:"POST",
				params
			}).then((res)=>{
				uni.navigateBack()
			})
		} else {
			if (JSON.stringify(Object.assign({}, oldAddress)) === JSON.stringify(Object.assign({},
				newAddressInformation))) {
				console.log("新旧一致");
				return
			} else {
				const params = {
						ad:newAddressInformation.detailAddress,
						def:newAddressInformation.isDefault,
						gender:newAddressInformation.gender,
						id: updateAddressId,
						name:newAddressInformation.name,
						phone:newAddressInformation.tel,
						userId:store.state.user.id
					}
					console.log(params);
				request({
					url: "/address",
					method: "PUT",
					params,
				}).then((res) => {
					uni.navigateBack()
				})
			}
		}

	}

	onLoad((options) => {
		if (options.addressId !== "[object Object]") {
			//获取对应Id的全部新西回填
			request({
				url: "/address/" + options.addressId,
				method: "GET"
			}).then((res) => {
				updateAddressId = res.id;
				newAddressInformation.detailAddress = res.ad;
				newAddressInformation.gender = res.gender;
				newAddressInformation.isDefault = res.def;
				newAddressInformation.name = res.name;
				newAddressInformation.tel = res.phone;
				oldAddress = Object.assign({}, newAddressInformation)
			})
			addOrChange.value = "change"
		} else {
			addOrChange.value = "add"
		}
	})
</script>

<style lang="scss" scoped>
	@import "@/static/css/commen.scss";

	.container {
		position: relative;
	}

	.content {


		.form {
			display: flex;
			flex-direction: column;
			padding: 32rpx;
			background: #FFFFFF;
			margin-top: 32rpx;
			border-radius: 24rpx;
			gap: 66rpx;

			.item {
				position: relative;
				display: flex;
				@include font-base(24rpx, #666666, 400);
				align-items: center;

				view {
					@include font-base(28rpx, #333333, 500);
					width: 180rpx;
				}
			}

			.item::after {
				content: "";
				width: 100%;
				height: 2rpx;
				background: #E6E6E6;
				position: absolute;
				bottom: -32rpx;
				left: 0;
			}

			.item:nth-last-child(1)::after {
				display: none;
			}

			.default-address {
				justify-content: space-between;
			}

			switch {
				width: 102rpx;
				height: 62rpx;
			}

		}

		.button-group {
			display: flex;
			gap: 24rpx;
			position: absolute;
			width: calc(100% - 64rpx);

			bottom: 0;

			button {
				margin: 0;
				background: transparent;
				border: 2rpx solid #333333;
				border-radius: 52rpx;
				@include font-base(32rpx, #333333, 400);
				display: flex;
				justify-content: center;
				align-items: center;
			}

			button::after {
				border: none;
			}

			.delete {
				width: 220rpx;
				height: 104rpx;
			}

			.submit {
				border-color: #FFFFFF;
				width: 100%;
			}

			.submit::after {
				z-index: -9;
				background: linear-gradient(to right, #FFD93B, #FFB42D);
			}
		}
	}
</style>